<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - lz77_buffer_kernel_c.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2003  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_LZ77_BUFFER_KERNEl_C_
<font color='#0000FF'>#define</font> DLIB_LZ77_BUFFER_KERNEl_C_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='lz77_buffer_kernel_abstract.h.html'>lz77_buffer_kernel_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../assert.h.html'>../assert.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> lz77_base
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>class</font> <b><a name='lz77_buffer_kernel_c'></a>lz77_buffer_kernel_c</b> : <font color='#0000FF'>public</font> lz77_base
    <b>{</b>
        
        <font color='#0000FF'>public</font>:
        <b><a name='lz77_buffer_kernel_c'></a>lz77_buffer_kernel_c</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> total_limit,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> lookahead_limit            
        <font face='Lucida Console'>)</font>;

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <b><a name='lookahead_buffer'></a>lookahead_buffer</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <b><a name='history_buffer'></a>history_buffer</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

        <font color='#0000FF'><u>void</u></font> <b><a name='shift_buffers'></a>shift_buffers</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> N
        <font face='Lucida Console'>)</font>;



        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='make_safe'></a>make_safe</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> total_limit,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> lookahead_limit
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - if ( 6 &lt; total_limit &lt; 32 &amp;&amp; 
                       15 &lt; lookahead_limit &lt;= 2^(total_limit-2) 
                       ) then
                    - returns total_limit
                - else
                    - throws due to failed CASSERT
        !*/</font>
        <b>{</b>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> exp_size <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>total_limit<font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>?total_limit<font color='#5555FF'>-</font><font color='#979000'>2</font>:<font color='#979000'>0</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> two_pow_total_limit_minus_2 <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>exp_size <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#5555FF'>-</font><font color='#5555FF'>-</font>exp_size;
                two_pow_total_limit_minus_2 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>;            
            <b>}</b>

            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font> <font color='#979000'>6</font> <font color='#5555FF'>&lt;</font> total_limit <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> total_limit <font color='#5555FF'>&lt;</font> <font color='#979000'>32</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                    <font color='#979000'>15</font> <font color='#5555FF'>&lt;</font> lookahead_limit <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> lookahead_limit <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> two_pow_total_limit_minus_2,
                "<font color='#CC0000'>\tlz77_buffer::lz77_buffer(unsigned long,unsigned long)</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\ttotal_limit must be in the range 7 to 31 and \n\tlookahead_limit in the range 15 to 2^(total_limit-2)</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:            </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\ttotal_limit:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> total_limit
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tlookahead_limit: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> lookahead_limit
                <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>return</font> total_limit;
        <b>}</b>

    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// member function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> lz77_base
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> lz77_buffer_kernel_c<font color='#5555FF'>&lt;</font>lz77_base<font color='#5555FF'>&gt;</font>::
    <b><a name='shift_buffers'></a>shift_buffers</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> N
    <font face='Lucida Console'>)</font> 
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font> N <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_lookahead_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\tvoid lz77_buffer::shift_buffers(unsigned long)</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tN must be &lt;= the number of chars in the lookahead buffer</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:                        </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tget_lookahead_buffer_size(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_lookahead_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tN:                           </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> N
            <font face='Lucida Console'>)</font>;

        <font color='#009900'>// call the real function
</font>        lz77_base::<font color='#BB00BB'>shift_buffers</font><font face='Lucida Console'>(</font>N<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> lz77_base
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> lz77_buffer_kernel_c<font color='#5555FF'>&lt;</font>lz77_base<font color='#5555FF'>&gt;</font>::
    <b><a name='history_buffer'></a>history_buffer</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font> index <font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_history_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\tunsigned char lz77_buffer::history_buffer(unsigned long) const</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tindex must be in the range 0 to get_history_buffer_size()-1</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:                      </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tget_history_buffer_size(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_history_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tindex:                     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> index
            <font face='Lucida Console'>)</font>;

        <font color='#009900'>// call the real function
</font>        <font color='#0000FF'>return</font> lz77_base::<font color='#BB00BB'>history_buffer</font><font face='Lucida Console'>(</font>index<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> lz77_base
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> lz77_buffer_kernel_c<font color='#5555FF'>&lt;</font>lz77_base<font color='#5555FF'>&gt;</font>::
    <b><a name='lookahead_buffer'></a>lookahead_buffer</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font> index <font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_lookahead_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\tunsigned char lz77_buffer::lookahead_buffer(unsigned long) const</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tindex must be in the range 0 to get_lookahead_buffer_size()-1</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:                        </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tget_lookahead_buffer_size(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_lookahead_buffer_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tindex:                       </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> index
            <font face='Lucida Console'>)</font>;

        <font color='#009900'>// call the real function
</font>        <font color='#0000FF'>return</font> lz77_base::<font color='#BB00BB'>lookahead_buffer</font><font face='Lucida Console'>(</font>index<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> lz77_base
        <font color='#5555FF'>&gt;</font>
    lz77_buffer_kernel_c<font color='#5555FF'>&lt;</font>lz77_base<font color='#5555FF'>&gt;</font>::
    <b><a name='lz77_buffer_kernel_c'></a>lz77_buffer_kernel_c</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> total_limit,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> lookahead_limit  
    <font face='Lucida Console'>)</font> :
        lz77_base<font face='Lucida Console'>(</font>make_safe<font face='Lucida Console'>(</font>total_limit,lookahead_limit<font face='Lucida Console'>)</font>,lookahead_limit<font face='Lucida Console'>)</font>
    <b>{</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_LZ77_BUFFER_KERNEl_C_
</font>

</pre></body></html>